package com.happy.controller;

import com.happy.model.dto.CardAddDTO;
import com.happy.model.dto.CardPageQuery;
import com.happy.model.dto.CardUpdateDTO;
import com.happy.model.result.PageResult;
import com.happy.model.result.Result;
import com.happy.model.vo.CardListByIdVO;
import com.happy.model.vo.CardListVO;
import com.happy.service.CardService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/card")
public class CardController {

    private final CardService cardService;

    public CardController(CardService cardService) {
        this.cardService = cardService;
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @GetMapping
    public Result<PageResult<CardListVO>> page(CardPageQuery query) {
        PageResult<CardListVO> pageResult = cardService.getPage(query);
        return Result.success(pageResult);
    }

    /**
     * 新增
     * @param cardAddDTO
     * @return
     */
    @PostMapping
    public Result<?> addCard(@RequestBody CardAddDTO cardAddDTO) {
        cardService.addCard(cardAddDTO);
        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<?> deleteCard(@RequestParam("id") Integer id) {
        cardService.deleteCard(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<CardListByIdVO> getCardById(@PathVariable("id") Integer id){
        return Result.success(cardService.getCardListById(id));
    }

    @PutMapping
    public Result<?> updateCard(@RequestBody CardUpdateDTO cardUpdateDTO){
        cardService.updateCard(cardUpdateDTO);
        return Result.success();
    }
}
